package cn.ekuma.epos.datalogic.local.erp.dao;

import java.util.Date;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import cn.ekuma.epos.bean.util.erp.FindOrdersInfoHelper;
import cn.ekuma.epos.datalogic.local.AbstractBaseDAO;
import cn.ekuma.epos.datalogic.util.DataLogicUtil;


public class FindOrdersInfoDAO  extends AbstractBaseDAO<FindOrdersInfo>{
	
	

	public FindOrdersInfoDAO(SQLiteOpenHelper db) {
		super(db);
	}
	
	public static void onCreate(SQLiteDatabase sqLiteDatabase) {
	    sqLiteDatabase.execSQL(
	    		"CREATE TABLE find_order_info("+
	    		"ID VARCHAR(255) NOT NULL,"+
	    		"orderid VARCHAR(255) NOT NULL,"+
	    		"ordertype INTEGER NOT NULL DEFAULT 0,"+
	    		"defineType VARCHAR(1024) NOT NULL,"+
	    		"date TIMESTAMP,"+
	    		"location VARCHAR(255) NOT NULL,"+
	    		"name VARCHAR(255) NOT NULL,"+
	    		"customer VARCHAR(255),"+
	    		"totalPrice DOUBLE PRECISION,"+
	    		"memo VARCHAR(255),"+
	    		"currentState VARCHAR(255) NOT NULL,"+
	    		"m_sResponse VARCHAR(255),"+
	    		"m_dPriceRate DOUBLE PRECISION,"+
	    		"locationRef1 VARCHAR(255),"+
	    		"debt DOUBLE PRECISION,"+
	    		"m_dRefDate TIMESTAMP,"+
	    		"content BLOB "+
	    		");");
	}

	@Override
	public String getKeyColumn() {
		return ID;
	}

	@Override
	public ContentValues createContentValues(FindOrdersInfo t) {
		ContentValues value=new ContentValues();
		value.put(ID, t.getId());
		value.put("orderid", t.getOrderid());
		value.put("ordertype", t.getOrdertype());
		value.put("defineType", t.getDefineType());
		value.put("date", t.getDate().getTime());
		value.put("location", t.getLocation());
		value.put("name", t.getName());
		value.put("customer", t.getCustomer());
		value.put("totalPrice", t.getTotalPrice());
		value.put("memo", t.getMemo());
		value.put("currentState", t.getCurrentState());
		value.put("m_sResponse", t.getM_sResponse());
		value.put("m_dPriceRate", t.getM_dPriceRate());
		value.put("locationRef1", t.getLocationRef1());
		value.put("debt", t.getDebt());
		value.put("m_dRefDate", t.getM_dRefDate()==null?0:t.getM_dRefDate().getTime());
		return value;
	}

	@Override
	public Class getSuportClass() {
		return FindOrdersInfo.class;
	}

	@Override
	public String getTableName() {
		return "find_order_info";
	}

	@Override
	public FindOrdersInfo readValues(Cursor c, FindOrdersInfo obj) {
		if(obj==null)
			obj=new FindOrdersInfo();
		obj.setId(c.getString(0));
		obj.setOrderid(c.getString(1));
		obj.setOrdertype(c.getInt(2));
		obj.setDefineType(c.getString(3));
		obj.setDate(new Date(c.getLong(4)));
		obj.setLocation(c.getString(5));
		obj.setName(c.getString(6));
		obj.setCustomer(c.getString(7));
		obj.setTotalPrice(c.getDouble(8));
		obj.setMemo(c.getString(9));
		obj.setCurrentState(c.getString(10));
		obj.setM_sResponse(c.getString(11));
		obj.setM_dPriceRate(c.getDouble(12));
		obj.setLocationRef1(c.getString(13));
		obj.setDebt(c.getDouble(14));
		obj.setM_dRefDate(new Date(c.getLong(15)));
		return obj;
	}
	
	@Override
	public String[] getTableColumns() {
		return new String[]{
				ID,
				"orderid",
				"ordertype",
				"defineType",
				"date",
				"location",
				"name",
				"customer",
				"totalPrice",
				"memo",
				"currentState",
				"m_sResponse",
				"m_dPriceRate",
				"locationRef1",
				"debt",
				"m_dRefDate"		
		};
	}
	
	public void save(List<Order> ords){
		SQLiteDatabase db = null;
        try {
          db = helper.getWritableDatabase();      
          // Insert the new entry into the DB.
          for(Order order:ords)
            db.insert(getTableName(), null, getOrderContentValues(order));
          
        } finally {
          close(null, db);
        }
	}
	
    public long save(Order order){
        SQLiteDatabase db = null;
        try {
          db = helper.getWritableDatabase();      
          // Insert the new entry into the DB.
          return db.insert(getTableName(), null, getOrderContentValues(order));
        } finally {
          close(null, db);
        }
	} 
    
    private ContentValues getOrderContentValues(Order o){
    	FindOrdersInfo orderInfo=FindOrdersInfoHelper.copyFor(o);
    	ContentValues conValues=createContentValues(orderInfo);
    	conValues.put("content", DataLogicUtil.writeSerializable(o));
    	return conValues;
    }
    
    public Order load(String id){
    	SQLiteDatabase db = null;
	    Cursor cursor = null;
	    try {
	      db = helper.getReadableDatabase();
	      cursor = db.query(getTableName(), 
	    		  new String[]{"content"},
	    		  getKeyColumn() + "=?",
                  new String[] {id}, null, null, null);
	      cursor.moveToFirst();
	      return  (Order) DataLogicUtil.readSerializable(cursor.getBlob(0));
	    } finally {
	      close(cursor, db);
	   }
    }
    
}
